package leetcode.stack;

import java.util.Stack;

/**
 * @author mazouri
 * @create 2021-08-07 12:02
 *
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 *
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 *
 */
public class MyQueue {
    private Stack<Integer> stackPush;
    private Stack<Integer> stackPop;

    /** Initialize your data structure here. */
    public MyQueue() {
        stackPush = new Stack<>();
        stackPop = new Stack<>();
    }

    private void pushToPop(){
        if (stackPop.isEmpty()){
            while (!stackPush.isEmpty()){
                stackPop.push(stackPush.pop());
            }
        }
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stackPush.push(x);
        pushToPop();
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (stackPop.isEmpty()&&stackPush.isEmpty()){
            throw new RuntimeException("empty");
        }
        pushToPop();
        return stackPop.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (stackPop.isEmpty()&&stackPush.isEmpty()){
            throw new RuntimeException("empty");
        }
        pushToPop();
        return stackPop.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stackPop.isEmpty()&&stackPush.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1); // queue is: [1]
        myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
        myQueue.peek(); // return 1
        myQueue.pop(); // return 1, queue is [2]
        myQueue.empty(); // return false
    }
}
